package dao;

import Utils.JDBCUtils;
import beans.User;
import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class userDao {
    public User login(User user) {
        User existUser = null;
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        try {
            existUser = qr.query("SELECT * FROM `user` WHERE username = ? AND password = ?",
                                    new BeanHandler<User>(User.class),
                                user.getUsername(), user.getUsername().equals("admin")?user.getPassword(): DigestUtil.md5Hex(user.getPassword()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return existUser;
    }
}
